package com.itheima.service.impl;

import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
@Service(interfaceClass=OrderService.class)
@Transactional


public class OrderServiceImpl implements OrderService {
    //本地注入
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;


    //预约设置
    @Override
    public Result order(Map map) throws Exception {

        map.put("orderType",Order.ORDERTYPE_WEIXIN);
        //根据日期查看当天有没有预约设置-------------------------------------------------------------------------------------
        //获取预约日期 查看有没有进行预约设置
        String orderDate =(String) map.get("orderDate");
        //根据工具类 进行日期的转换 进行查询
       OrderSetting orderSetting= orderSettingDao.findByOrderDate(DateUtils.parseString2Date(orderDate));
        //如果当天查询结果预约结果为空,表示当天没有进行预约设置
       if(orderSetting==null){
         return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
       }
       //根据与预约人数与预约总数相比,查看是否可以继续预约-----------------------------------------------------------------------
        //预约总数
        int number = orderSetting.getNumber();
       //已预约的人数
        int reservations = orderSetting.getReservations();
       //进行人数判断
        if(reservations>=number){
            return new Result(false, MessageConstant.ORDER_FULL);
        }
       //检查用户手机号是否是会员(是否存在)-------------------------------------------------------------------------------------------
        String telephone =(String)map.get("telephone");
        Member member = memberDao.findByTelephone(telephone);
        if(member!= null){
         // 手机号存在 是会员 封装本次预约信息 查看是否重复预约
            Integer id = member.getId();
           Date order_Date= DateUtils.parseString2Date(orderDate);
            String setmealId =(String)map.get("setmealId");
         Order order=new Order(id,order_Date,Integer.parseInt(setmealId));
         //根据用户id 预定日期 套餐id 来查询 查看是否重复预约
            List<Order> list = orderDao.findByCondition(order);
            if (list!=null&&list.size()>0){
                //已经重复预约
                return new Result(false, MessageConstant.HAS_ORDERED);
            }
        }else {
            //  手机号不存在 不是会员 要为其注册会员
            member = new Member();
            member.setName((String) map.get("name"));
            member.setPhoneNumber(telephone);
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            member.setRegTime(new Date());
            memberDao.add(member);
        }
        //注册成功-------------------------------------------------------------------------------------------------------
        Order order=new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(DateUtils.parseString2Date(orderDate));
        order.setOrderType((String)map.get("orderType"));
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setSetmealId(Integer.parseInt((String)map.get("setmealId")));
        orderDao.add(order);

         //预约人数+1
        orderSetting.setReservations(orderSetting.getReservations()+1);
        //更新预约人数
        orderSettingDao.editReservationsByOrderDate(orderSetting);
        //预约设置成功 返回预约消息
        return new Result(true, MessageConstant.ORDER_SUCCESS,order);
    }

    //查询预约信息
    @Override
    public Map findById(Integer id) throws Exception {
      Map map = orderDao.findById4Detail(id);
      //处理日期
      if(map!=null){
         Date orderDate = (Date) map.get("orderDate");
          map.put("orderDate", DateUtils.parseDate2String(orderDate));
      }
        return map;
    }
}
